/*
 *  message.cpp
 */

#include <string>
#include <vector>
#include "debug.h"
#include "message.h"
#include "util.h"

Message::Message(std::string str_msg) {
    std::string separator;
    separator += Message::CODE_GROUP_SEPARATOR;
    std::vector<std::string>* v = Util::split(str_msg, separator);
    this->from = (*v)[0];
    this->type = (*v)[1];
    this->body = (*v)[2];
    delete v;
}

Message::Message(std::string from, std::string type) : body("") {
    this->from = from;
    this->type = type;
}

Message::Message(std::string from, std::string type, std::string record) {
    this->from = from;
    this->type = type;
    this->body = record;
}

Message::~Message() {}

std::string Message::getFrom() {
    return from;
}

std::string Message::getType() {
    return type;
}

std::string Message::toString() {
    return from
            + CODE_GROUP_SEPARATOR
            + type
            + CODE_GROUP_SEPARATOR
            + body;
}

void Message::addRecord(std::string record) {
    if (body != "") body +=  CODE_RECORD_SEPARATOR;
    body += record;
}

void Message::addRecord(std::string units[], int size) {
    if (size <= 0) return;
    if (body != "") body +=  CODE_RECORD_SEPARATOR;
    body += units[0];
    for (int i = 1; i < size; ++i) body += CODE_UNIT_SEPARATOR + units[i];
}

std::vector<std::string>* Message::getRecords() {
    if (body == "") return NULL;
    std::string separator;
    separator += Message::CODE_RECORD_SEPARATOR;
    return Util::split(body, separator);
}

std::vector<std::string>* Message::getUnits(std::string record) {
    if (record == "") return NULL;
    std::string separator;
    separator += Message::CODE_UNIT_SEPARATOR;
    return Util::split(record, separator);
}

//{+}

